<?php

class form_fields {
	
	function textfield ($field_name, $label, $hint = '', $default_value = '', $class = "blockize", $lang = false) {
		$out  = '<div class="'.$this->_get_div_classname($lang).'">';
		$out .= '<label for="'. $this->_get_id($field_name, $lang) .'">'. $label .'</label>';
		$out .= '<input type="text" id="'. $this->_get_id($field_name, $lang) .'" name="'. $this->_get_field_name($field_name, $lang) .'" value="'. $this->_get_default_value($default_value, $lang) .'" class="'. $class .'" />';
		if ($hint != '') {
			$out .= '<div class="hint">'.$hint.'</div>';
		}
		$out .= '</div>';
		return $out;
	}
	
	function password ($field_name, $label, $hint = '', $default_value = '', $class = "blockize", $lang = false) {
		$out  = '<div class="'.$this->_get_div_classname($lang).'">';
		$out .= '<label for="'. $this->_get_id($field_name, $lang) .'">'. $label .'</label>';
		$out .= '<input type="password" id="'. $this->_get_id($field_name, $lang) .'" name="'. $this->_get_field_name($field_name, $lang) .'" value="'. $this->_get_default_value($default_value, $lang) .'" class="'. $class .'" />';
		if ($hint != '') {
			$out .= '<div class="hint">'.$hint.'</div>';
		}
		$out .= '</div>';
		return $out;
	}
	
	function datefield ($field_name, $label, $hint = '', $default_value = '', $class = "", $lang = false) {
		$GLOBALS['Template']->include_module('calendar');
		$cal = new calendar();
		$out  = '<div class="'.$this->_get_div_classname($lang).'">';
		$out .= '<label for="'. $this->_get_id($field_name, $lang) .'">'. $label .'</label>';
		$out .= '<input type="text" id="'. $this->_get_id($field_name, $lang) .'" name="'. $this->_get_field_name($field_name, $lang) .'" value="'. $this->_get_default_value($default_value, $lang) .'" class="'. $class .'" /> ';
		$out .= $cal->insert($field_name);
		if ($hint != '') {
			$out .= '<div class="hint">'.$hint.'</div>';
		}
		$out .= '</div>';
		return $out;
	}
	
	function textarea ($field_name, $label, $hint = '', $default_value = '', $class = "blockize", $lang = false) {
		$out  = '<div class="'.$this->_get_div_classname($lang).'">';
		$out .= '<label for="'. $this->_get_id($field_name, $lang) .'">'. $label .'</label>';
		$out .= '<textarea id="'. $this->_get_id($field_name, $lang) .'" name="'. $this->_get_field_name($field_name, $lang) .'" class="'. $class .'">'. $this->_get_default_value($default_value, $lang) .'</textarea>';
		if ($hint != '') {
			$out .= '<div class="hint">'.$hint.'</div>';
		}
		$out .= '</div>';
		return $out;
	}
	
	function fck($field_name, $label, $hint = '', $default_value = '', $class = "blockize", $lang = false) {
		$GLOBALS['Template']->include_module('fckeditor');
		$out .= '<div class="'.$this->_get_div_classname($lang).'">';
		$out .= '<label for="'. $this->_get_id($field_name, $lang) .'">'. $label .'</label>';
		$fck = new FCKeditor($this->_get_field_name($field_name, $lang));
		// pecionata!
		$fck->Width = 680;
		$fck->Height = 350;
		$fck->Value = $this->_get_default_value($default_value, $lang);
		$out .= $fck->CreateHtml();
		$out .= '</div>';
		return $out;
	}
	
	function file ($field_name, $label, $hint = '', $default_value = '', $class = "blockize", $lang = false) {
		$out  = '<div class="'.$this->_get_div_classname($lang).'">';
		$out .= '<label for="'. $this->_get_id($field_name, $lang) .'">'. $label;
		$out .= '</label>';
		$out .= '<input type="file" id="'. $this->_get_id($field_name, $lang) .'" name="'. $this->_get_field_name($field_name, $lang) .'" class="'. $class .'" />';
		
		if ($default_value != '' || $hint != '') {
			$out .= '<div class="hint">';
			if ($hint != '') {
				$out .= $hint;
			}
			if ($default_value != '' && $hint != '') {
				$out .= '<br />';
			}
			if ($default_value != '') {
				$out .= 'Scegli un file solo se hai intenzione di modificare quello attuale: <a href="'. $default_value .'" target="_blank">'. basename($default_value) .'</a>';
			}
			$out .= '</div>';
		}
		
		$out .= '</div>';
		return $out;		
	}
	
	function select ($field_name, $label, $hint = '', $values = array(), $default_value = '', $class="blockize", $lang = false) {
		$out  = '<div class="'.$this->_get_div_classname($lang).'">';
		$out .= '<label for="'. $this->_get_id($field_name, $lang) .'">'. $label .'</label>';
		$out .= '<select name="'.$this->_get_field_name($field_name, $lang).'" id="'. $this->_get_id($field_name, $lang) .'" class="'.$class.'">';
		if (count($values) > 0) {
			foreach($values as $k => $v) {
				$sel = ($k == $default_value) ? ' selected="selected"' : '';
				$out .= '<option value="'. $k .'"'. $sel .'>'. $v .'</option>';
			}
		}
		$out .= '</select>';
		if ($hint != '') {
			$out .= '<div class="hint">'.$hint.'</div>';
		}
		$out .= '</div>';
		return $out;
	}
	
	function hidden ($field_name, $label, $hint = '', $default_value = '', $class = "blockize", $lang = false) {
		$out  = '<input type="hidden" value="'. $default_value .'" id="'. $this->_get_id($field_name, $lang) .'" name="'. $this->_get_field_name($field_name, $lang) .'" />';
		return $out;
	}
	
	function free_txt ($txt, $lang = false) {
		return '<div class="'.$this->_get_div_classname($lang).'">'. $txt .'</div>';
	}

	function submit ($label = 'Salva', $back_url = false) {
		$out  = '<div class="submit"><input type="submit" value="'. $label .'" name="submit" />';
		if ($back_url) {
			$out .= ' <input type="button" name="back" value="Annulla e torna indietro" onclick="window.location=\''. $back_url .'\'" />';
		}
		$out .= '</div>';
		return $out;
	}
	
	function _get_div_classname ($lang) {
		if ($lang) return 'row lang '.$lang;
		return 'row';
	}
	
	function _get_id ($field_name, $lang) {
		if ($lang) return $field_name.'_'.$lang;
		return $field_name;
	}
	
	function _get_field_name ($field_name, $lang) {
		if ($lang) return $field_name.'['.$lang.']';
		return $field_name;
	}
	
	function _get_default_value ($default_value, $lang) {
		if (is_array($default_value) && $lang) {
			return $default_value[$lang];
		}
		return $default_value;
	}

}

?>